#import "CTUserManager.h"
#import "SSKeychain.h"
#import "CTControllerManager.h"
@interface CTUserManager ()
@property (nonatomic ,copy) NSString *locationStatusKey;
@end
@implementation CTUserManager
+ (instancetype)shareManager
{
    static CTUserManager *manager;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        manager = [[super allocWithZone:NULL] init];
    });
    return manager;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
    return [CTUserManager shareManager];
}
- (id)copyWithZone:(struct _NSZone *)zone {
    return [CTUserManager shareManager];
}
- (instancetype)init{
    if (self = [super init]) {
    }
    return self;
}
- (void)setHasLogined:(BOOL)hasLogined{
    [[NSUserDefaults standardUserDefaults] setBool:hasLogined forKey:@"loginStatus"];
}
- (BOOL)hasLogined{
    BOOL hasLogined = [[NSUserDefaults standardUserDefaults] boolForKey:@"loginStatus"];
    return hasLogined;
}
- (CTUserModel *)userModel {
    if (!_userModel) {
        NSDictionary *userDic = [CTDBManager getInDocumentWithObjectByFileName:NSStringFromClass(CTUserModel.class)];
        _userModel = [CTUserModel modelWithDictionary:userDic];
    }
    return _userModel;
}
- (void)updateUserInfo:(CTUserModel *)userModel
{
    NSDictionary *userDic = [userModel mj_keyValues];
    NSDictionary *getDic = [CTDBManager getInDocumentWithObjectByFileName:NSStringFromClass(userModel.class)];
    [CTDBManager removeInDocumentWithObjectByFileName:NSStringFromClass(userModel.class)];
    [CTDBManager saveInDocumentWithObject:userDic byFileName:NSStringFromClass(userModel.class)];
    [CTUserManager shareManager].userModel = userModel;
}
- (void)userLogout {
    self.userModel = nil;
    [self setHasLogined:NO];
    [CTControllerManager ct_configRootVC];
}
- (void)userLogin:(CTUserModel *)userModel {
    self.userModel = userModel;
    [self updateUserInfo:userModel];
    [self loginSuccess];
}
- (void)loginSuccess {
    [self configLoginStatus];
    [CTControllerManager ct_configRootVC];
}
- (void)configLoginStatus {
    if (self.userModel.userAuthenticationToken.length > 0) {
        if (!self.hasLogined) {
            self.hasLogined = YES;
        }
    }else{
        self.hasLogined = NO; 
    }
}
- (NSString *)token {
    return self.userModel.userAuthenticationToken;
}
+ (BOOL)appIsFirstLaunched
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        return YES;
    }
    else{
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        return NO;
    }
}
- (NSString *)ct_getTokenId {
    NSString *uuid = [NSUUID UUID].UUIDString;
    return uuid;
}
+ (NSString *)ct_getZuid {
    NSString *zuid = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"keychain_zuid"];
    if (beNil(zuid)) {
        NSString *uuid = [NSUUID UUID].UUIDString;
        NSString *zuid = [NSString stringWithFormat:@"%@_123420240710",uuid];
        zuid = [[zuid lowercaseString] stringByReplacingOccurrencesOfString:@"-" withString:@""];
        [SSKeychain setPassword:zuid forService:[NSBundle mainBundle].bundleIdentifier account:@"keychain_zuid"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        return zuid;
    }
    return zuid;
}
+(NSString *)ct_getCid
{
    NSString *cid = [SSKeychain passwordForService:[NSBundle mainBundle].bundleIdentifier account:@"keychain_Cid"];
    if (beNil(cid)) {
        CFUUIDRef uuid = CFUUIDCreate(NULL);
        assert(uuid != NULL);
        CFStringRef uuidStr = CFUUIDCreateString(NULL, uuid);
        NSString *tempUID = [NSString stringWithFormat:@"%@", uuidStr];
        cid = [self md5HexDigest:tempUID];
        [SSKeychain setPassword:cid forService:[NSBundle mainBundle].bundleIdentifier account:@"keychain_Cid"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    return cid;
}
+ (NSString *)md5HexDigest:(NSString *)input {
    const char *inputCString = [input UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(inputCString, (CC_LONG)strlen(inputCString), result);
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [output appendFormat:@"%02x", result[i]];
    }
    return output;
}
+ (void)cachePhone:(NSString *)phone {
    NSString *service = [NSString stringWithFormat:@"%@%@",[NSBundle mainBundle].bundleIdentifier, phone];
    [SSKeychain setPassword:phone forService:service account:@"phone"];
}
+ (BOOL)checkPhoneHasLogged:(NSString *)phone {
    NSString *service = [NSString stringWithFormat:@"%@%@",[NSBundle mainBundle].bundleIdentifier, phone];
    NSString *phoneString = [SSKeychain passwordForService:service account:@"phone"];
    if (phoneString == nil || phoneString.length == 0 || [phoneString isEqualToString:@""]) {
        return NO;
    } else {
        return YES;
    }
}
@end
